/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__frct_timer_callback.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/17 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frtimer.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frtimer.h"
#include "frct_comm.h"
#include "frct_hash.h"
#include "frct_mng.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

extern pthread_spinlock_t   g_spin_ct_lock;

void __frct_timer_callback(int i_fd, short s_event, void *p_arg);
extern struct event_base *g_p_st_ebase;
extern int g_i_flag_run;
extern Frhash_header        *g_p_st_hash_ct;
void __hash_node_free(Frlist_node *p_st_node);


static void __case1(void)
{
    int i_fd = 1;
    short s_event = 1;
    Conntrack *p_st_ct = NULL;
    Frlist_node *p_st_node = NULL;
    Timer_entry *p_st_timer_entry = NULL;

    frct_hash_init(65536);
    g_p_st_ebase = event_base_new();
    frtimer_init();

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_update = FR_YES;
    pthread_spin_init(&p_st_ct->lock, 0);

    p_st_timer_entry = (Timer_entry *)frmalloc(sizeof(Timer_entry));
    memset(p_st_timer_entry, 0x00, sizeof(Timer_entry));
    p_st_ct->p_st_timer_entry = p_st_timer_entry;

    (p_st_timer_entry->st_delay).tv_sec = 1000;
    (p_st_timer_entry->st_delay).tv_usec = 0;

    p_st_node = frlist_node_create(p_st_ct);

    __frct_timer_callback(i_fd,s_event,p_st_node);

    if(p_st_timer_entry->p_st_event)
        frtimer_free(p_st_timer_entry->p_st_event);
    if(p_st_ct)
        frfree(p_st_ct);
    if(p_st_node)
        frfree(p_st_node);
    if(p_st_timer_entry)
        frfree(p_st_timer_entry);

    event_base_free(g_p_st_ebase);
    frct_hash_destory(__hash_node_free);

}

static void __case2(void)
{
    int i_fd = 1;
    short s_event = 1;
    Conntrack *p_st_ct = NULL;
    Frlist_node *p_st_node = NULL;
    Timer_entry *p_st_timer_entry = NULL;

    frct_hash_init(5);
    g_p_st_ebase = event_base_new();
    frtimer_init();

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_update = FR_NO;
    p_st_ct->refcnt = 1;
    pthread_spin_init(&p_st_ct->lock, 0);

    p_st_timer_entry = (Timer_entry *)frmalloc(sizeof(Timer_entry));
    memset(p_st_timer_entry, 0x00, sizeof(Timer_entry));
    p_st_ct->p_st_timer_entry = p_st_timer_entry;

    (p_st_timer_entry->st_delay).tv_sec = 1000;
    (p_st_timer_entry->st_delay).tv_usec = 0;

    p_st_node = frlist_node_create(p_st_ct);

    __frct_timer_callback(i_fd,s_event,p_st_node);

    if(p_st_timer_entry->p_st_event)
        frtimer_free(p_st_timer_entry->p_st_event);
    if(p_st_ct)
        frfree(p_st_ct);
    if(p_st_node)
        frfree(p_st_node);
    if(p_st_timer_entry)
        frfree(p_st_timer_entry);

    frct_hash_destory(__hash_node_free);

    event_base_free(g_p_st_ebase);

}

static void __case3(void)
{
    int i_fd = 1;
    short s_event = 1;
    Conntrack *p_st_ct = NULL;
    Frlist_node *p_st_node = NULL;
    Timer_entry *p_st_timer_entry = NULL;

    frct_hash_init(65536);
    g_p_st_ebase = event_base_new();
    frtimer_init();

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_update = FR_NO;
    p_st_ct->refcnt = 0;
    p_st_ct->flg_status = APM_CONNECT_TIMEOUT;
    pthread_spin_init(&p_st_ct->lock, 0);

    p_st_timer_entry = (Timer_entry *)frmalloc(sizeof(Timer_entry));
    memset(p_st_timer_entry, 0x00, sizeof(Timer_entry));
    p_st_ct->p_st_timer_entry = p_st_timer_entry;

    (p_st_timer_entry->st_delay).tv_sec = 1000;
    (p_st_timer_entry->st_delay).tv_usec = 0;

    p_st_node = frlist_node_create(p_st_ct);

    __frct_timer_callback(i_fd,s_event,p_st_node);


    if(p_st_timer_entry->p_st_event)
        frtimer_free(p_st_timer_entry->p_st_event);
    if(p_st_ct)
        frfree(p_st_ct);
    if(p_st_node)
        frfree(p_st_node);
    if(p_st_timer_entry)
        frfree(p_st_timer_entry);
    frct_hash_destory(__hash_node_free);

    event_base_free(g_p_st_ebase);
}

static void __case4(void)
{
    int i_fd = 1;
    short s_event = 1;
    Conntrack *p_st_ct = NULL;
    Frlist_node *p_st_node = NULL;
    Timer_entry *p_st_timer_entry = NULL;

    frct_hash_init(65536);
    g_p_st_ebase = event_base_new();
    frtimer_init();

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_update = FR_NO;
    p_st_ct->refcnt = 0;
    p_st_ct->flg_status = APM_CONNECT_CLOSED;
    pthread_spin_init(&p_st_ct->lock, 0);

    p_st_timer_entry = (Timer_entry *)frmalloc(sizeof(Timer_entry));
    memset(p_st_timer_entry, 0x00, sizeof(Timer_entry));
    p_st_ct->p_st_timer_entry = p_st_timer_entry;

    (p_st_timer_entry->st_delay).tv_sec = 1000;
    (p_st_timer_entry->st_delay).tv_usec = 0;

    p_st_node = frlist_node_create(p_st_ct);

    __frct_timer_callback(i_fd,s_event,p_st_node);


    if(p_st_timer_entry->p_st_event)
        frtimer_free(p_st_timer_entry->p_st_event);
    if(p_st_ct)
        frfree(p_st_ct);
    if(p_st_node)
        frfree(p_st_node);
    if(p_st_timer_entry)
        frfree(p_st_timer_entry);
    frct_hash_destory(__hash_node_free);

    event_base_free(g_p_st_ebase);
}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__frct_timer_callback 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", __case1);

   	CU_add_test(pSuite, "case2", __case2);

  	CU_add_test(pSuite, "case3", __case3);

  	CU_add_test(pSuite, "case4", __case4);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}